<?php
$pool = new \Swoole\Process\Pool(2);

$pool->on('workerStart', function (\Swoole\Process\Pool $pool, $workerId) {
    /** 当前是 Worker 进程 */
    static $running = true;
    \Swoole\Process::signal(SIGTERM, function () use (&$running) {
        $running = false;
        echo "TERM\n";
    });
    echo("[Worker #{$workerId}] WorkerStart, pid: " . posix_getpid() . "\n");

    $redis = new Redis();
    $redis->pconnect('redis', 6379);
    $key = "key1";
    while ($running) {
        $msg = $redis->brpop($key, 0);
        pcntl_signal_dispatch();
        if ( $msg == null) continue;
        print_r($msg) . PHP_EOL;
    }
});

$pool->on('WorkerStop', function (\Swoole\Process\Pool $pool, $workerId) {
    echo("[Worker #{$workerId}] WorkerStop\n");
});

$pool->start();